package com.company;

import net.sf.json.JSONArray;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by hehe on 2016/10/2.
 */
public class ReadHTML {
    File input = null;
    Document doc  = null;
    List<Data> datas = new ArrayList<Data>();

    String lifeIDs[][] = {
        {"装配任务","装配工具","装配工艺规划","整机检验及测试","产品涂装入库","产品装配标准化知识"},
            {"29","30","31","32","33","34"}

    };
    String themeIDs[][] = {{"产品对象类知识","工作原理类知识","产品标准化知识","结构设计与强度类知识","制造工程类知识","材料类知识","综合管理类知识"},
            {"57","75","76","77","78","91","93"}
    };
    String forms[][] = {{"技术文件","标准化文件","原理图","零件图","装配图","零件模型","装配模型","过程程序","过程分析结果","过程软件","数据类知识","经验类知识"},
            {"67","69","85","86","87","88","89","68","72","90","73","74"}};
    ReadHTML(){
        File input = new File("d:/x.htm");
        try {
            doc = Jsoup.parse(input, "gb2312", "");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<Data> writeHTMl(){
        Elements tables = doc.select("div[align=center]");
        for (Element table:
                tables ) {
            Data data = new Data();
            Elements trs = table.select("tr");
            String field = trs.get(0).select("td").get(1).text();
            //动力装备，写死
            data.field = "2";
            data.name = trs.get(1).select("td").get(1).text();

            String time = trs.get(2).select("td").get(1).text();
            for(int i = 0;  i<lifeIDs[0].length;i++){
                if(time.trim().equals(lifeIDs[0][i].trim())){
                    data.time = lifeIDs[1][i];
                }
            }

            if(data.time==null){
                data.time = trs.get(2).select("td").get(1).text();
            }

            String theme = trs.get(2).select("td").get(3).text();
            for(int i = 0;  i<themeIDs[0].length;i++){
                if(theme.trim().equals(themeIDs[0][i].trim())){
                    data.theme = themeIDs[1][i];
                }
            }
            if(data.theme==null){
                data.theme = trs.get(2).select("td").get(3).text();
            }

            String form =  trs.get(3).select("td").get(3).text();
            for(int i = 0; i< forms[0].length;i++){
                if(form.trim().equals(forms[0][i].trim())){
                    data.form= forms[1][i];
                }
            }
            if(data.form==null){
                data.form = trs.get(3).select("td").get(3).text();
            }
            data.keywords = trs.get(4).select("td").get(1).text();
            data.body = trs.get(9).select("td").get(1).text();
            datas.add(data);
            System.out.println(data.toString());
        }
        ListToJson();
        return datas;
    }
    public void ListToJson(){

        JSONArray  jsonArray = new JSONArray().fromObject(datas);
        System.out.println(jsonArray.toString());
        IO.write2Txt(jsonArray.toString(),"d:/a.json");
    }
    public Map<String, Object> Lis2Data(Data data){

        Map<String, Object> requestParamsMap = new HashMap<String, Object>();
        //获取当前时间
        Date now=new Date();
        SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        requestParamsMap.put("ClassID",data.getField());//所属领域
        requestParamsMap.put("TemplateID",data.getTemplateID());//模版选择
        requestParamsMap.put("Name",data.getName());//知识名称
        requestParamsMap.put("ID","0");//ID意义未知
        requestParamsMap.put("Url","111111");//标识符
        requestParamsMap.put("_parentId",data.getTime());//生命周期
        requestParamsMap.put("LifeID",","+data.getTime()+",");//生命周期
        requestParamsMap.put("_parentId",data.getTheme());//知识主题
        requestParamsMap.put("ThemeID",","+data.getTheme()+",");//知识主题
        requestParamsMap.put("Belongs","");//组件
        requestParamsMap.put("_parentId",data.getForm());//知识形式
        requestParamsMap.put("FormID",","+data.getForm()+",");//知识形式
        requestParamsMap.put("KeyWords",data.getKeywords());//关键字
        requestParamsMap.put("AuthorID","22");//作者编号
        requestParamsMap.put("VersionCode","V1.00");//版本号
        requestParamsMap.put("CreateTime",myFmt2.format(now));//创建时间
        requestParamsMap.put("Power","管理员");//作者权限
        requestParamsMap.put("Source","");
        requestParamsMap.put("AttachmentCodes","");
        requestParamsMap.put("Extend","");
        requestParamsMap.put("Abstract","");
        requestParamsMap.put("Pic","");
        requestParamsMap.put("Task","");
        requestParamsMap.put("Chart","");
        requestParamsMap.put("Tool","");
        requestParamsMap.put("Content","<p style=\"clear: both;\"><br/><span style=\"font-size:14px;font-family:宋体\">"+data.getBody()+"</span></p>");
        requestParamsMap.put("editorValue","<p style=\"clear: both;\"><br/><span style=\"font-size:14px;font-family:宋体\">"+data.getBody()+"</span></p>");
        return requestParamsMap;
    }


}
